<?php
define('IN_ECS', true);

require(dirname(__FILE__).'/includes/init.php');							//初始化文件

require_once ROOT_PATH . 'languages/' .$_CFG['lang'].'/user.php';			//加载语言文件

$user_id = $_SESSION['user_id'];											//用户id
$action = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'default';		//获得当前的 action

$not_login_arr 	= array('login','act_login','act_register','act_edit_password','get_password','send_pwd_email','password',);
$ui_arr 		= array('register','login','profile','default');

if(empty($_SESSION['user_id']))												//session 中无 user_id,用户没有登陆
{
	if(!in_array($action, $not_login_arr))									//不需要登陆的action
	{
	if(in_array($action, $ui_arr))		   									//需要显示界面的action
		{
			if(!empty($_SERVER['QUERY_STRING']))							//地址中有参数信息
			{
				$back_act = 'user.php?'.$_SERVER['QUERY_STRING'];			//地址中包含查询字符串
			}
			$action = 'login';												//action重设为login
		}
		else 																//不需要显示界面的action
		{
			die('非正常提交数据');
		}
	}
}

if(in_array($action, $ui_arr))												//需要显示界面的action
{
	$sql = "SELECT value FROM " . $ecs->table('shop_config') . " WHERE id = 419";	//商城参数
	$row = $db->getRow($sql);
	$car_off = $row['value'];
	$smarty->assign('car_off',$car_off);
	
	if(!empty($_CFG['points_rule']) && unserialize($_CFG['points_rule']))
	{
		$smarty->assign('show_transform_points',1);
	}
	
	$smarty->assign('data_dir',DATA_DIR);	
	$smarty->assign('action',$action);

}	
		
if($action == 'default')			//默认action,显示用户中心
{
	include_once ROOT_PATH . 'includes/lib_clips.php';
//	if($rank = get_rank_info())
//	{
//		$smarty->assign('rank_name',sprintf($_LANG['your_level'],$rank['rank_name']));
//		if(!empty($rank['next_rank_name']))
//		{
//			$smarty->assign('next_rank_name',sprintf($_LANG['next_level'],$rank['next_rank'],$rank['next_rank_name']));
//		}	
//	}
	
	$smarty->assign('info',		get_user_default($user_id));		//用户基本信息
	$smarty->assign('user_notice',$_CFG['user_notice']);			//用户通知
//	$smarty->assign('prompt',	get_user_prompt($user_id));			//促销信息
	$smarty->display('user_clips.dwt');								//用户中心欢迎页面				
	
}

if($action == 'login')				//显示用户登陆界面
{
	$smarty->assign('action',$action);
	$smarty->display('user_passport.dwt');
}
elseif($action == 'act_login')		//用户提交登陆请求
{
	$username = isset($_POST['username']) ? trim($_POST['username']) : '';
	$password = isset($_POST['password']) ? trim($_POST['password']) : '';
	$back_act = isset($_POST['back_act']) ? trim($_POST['back_act']) : '';
	
	if($user->login($username,$password,isset($_POST['remember'])))
	{
		show_message('登陆成功',array('返回上一页','用户中心'),array($back_act,'user.php'),'info');		
	}
	else 
	{
		$_SESSION['login_fail']++;
	
		echo '登陆失败';
	}
}
elseif($action == 'logout')
{
	if(!isset($back_act) && isset($GLOBALS['_SERVER']['HTTP_REFERER']))
	{
		$back_act = strpos($GLOBALS['_SERVER']['HTTP_REFERER'], 'user.php') ? './index.php' : $GLOBALS['_SERVER']['HTTP_REFERER'];
	}
	
	$user->logout();
	$ucdata = empty($user->ucdata) ? "" : $user->ucdata;
	show_message($_LANG['logout'] . $ucdata,array($_LANG['back_up_page'],$_LANG['back_home_lnk']),array($back_act,'index.php'),'info');
}

